﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrmPrincipal
{
    public partial class FrmFuncionarios : Form
    {
        string connectionString = @"Server=.\sqlexpress;Database=McDonald;Trusted_Connection=True;";
        bool novo;
        public FrmFuncionarios()
        {
            InitializeComponent();
        }

       
        public void DesativaCampos()
        {
            txtCargo.Enabled = false;
            txtID.Enabled = false;
            txtNome.Enabled = false;
            txtPesquisa.Enabled = false;
            mskCPF.Enabled = false;
            txtSalario.Enabled = false;
            mskTelefone.Enabled = false;
        }
        public void AtivaCampos()
        {
            txtCargo.Enabled = true;
            txtID.Enabled = true;
            txtNome.Enabled = true;
            txtPesquisa.Enabled = true;
            mskCPF.Enabled = true;
            txtSalario.Enabled = true;
            mskTelefone.Enabled = true;
        }
        public void DesativaBotoes()
        {
            btnCancelar.Enabled = false;
            btnExcluir.Enabled = false;
            btnNovo.Enabled = false;
            btnPesquisa.Enabled = false;
            btnSalvar.Enabled = false;
        }
        public void AtivaBotoes()
        {
            btnCancelar.Enabled = true;
            btnExcluir.Enabled = true;
            btnNovo.Enabled = true;
            btnPesquisa.Enabled = true;
            btnSalvar.Enabled = true;
        }
        public void LimpaCampos()
        {
            txtPesquisa.Text = "";
            txtNome.Text = "";
            mskCPF.Text = "";
            mskTelefone.Text = "";
            txtID.Text = "";
            txtCargo.Text = "";
            txtSalario.Text = "";
        }
        private void FrmFuncionarios_Load(object sender, EventArgs e)
        {
            DesativaCampos();
            DesativaBotoes();
            btnNovo.Enabled = true;
            btnPesquisa.Enabled = true;
            txtPesquisa.Enabled = true;
        }
        private void btnNovo_Click_1(object sender, EventArgs e)
        {
            novo = true;
            DesativaBotoes();
            LimpaCampos();
            AtivaCampos();
            btnCancelar.Enabled = true;
            btnSalvar.Enabled = true;
            txtID.Enabled = false;
            btnPesquisa.Enabled = false;
            txtPesquisa.Enabled = false;
            txtNome.Focus();
        }

        private void btnSalvar_Click_1(object sender, EventArgs e)
        {
            if (novo)
            {
                string sql = "INSERT INTO Funcionario (nm_Funcionario,cd_CPF,id_Telefone,nm_Cargo,id_Salario) "
                    + "VALUES ('" + txtNome.Text + "', '" + mskCPF.Text + "','" + mskTelefone.Text + "','"
                    + txtCargo.Text + "','" + txtSalario.Text + "')";
                SqlConnection con = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.CommandType = CommandType.Text;
                con.Open();
                try
                {
                    int i = cmd.ExecuteNonQuery();
                    if (i > 0)
                        MessageBox.Show("Cadastro realizado com sucesso!");
                }
                catch (Exception ex)
                { MessageBox.Show("Erro: " + ex.ToString()); }
                finally { con.Close(); }

            }

            else
            {
                string sql = "UPDATE Funcionario SET nm_Funcionario='" + txtNome.Text + "', cd_CPF='"
                    + mskCPF.Text + "', id_Telefone='" + mskTelefone.Text + "', nm_Cargo='"
                    + txtCargo.Text + "', id_Salario='" + txtSalario.Text + "'"
                    + "WHERE id_Funcionario = '" + txtID.Text + "'";
                SqlConnection con = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.CommandType = CommandType.Text; con.Open();
                try
                {
                    int i = cmd.ExecuteNonQuery();
                    if (i > 0) MessageBox.Show("Cadastro atualizado com sucesso!");
                }
                catch (Exception ex)
                { MessageBox.Show("Erro: " + ex.ToString()); }
                finally { con.Close(); }
            }
            DesativaCampos();
            LimpaCampos();
            AtivaBotoes();
            btnExcluir.Enabled = false;
            btnCancelar.Enabled = false;
            txtPesquisa.Enabled = true;
            btnPesquisa.Enabled = true;
            btnSalvar.Enabled = false;
        }

        private void btnExcluir_Click_1(object sender, EventArgs e)
        {
            string sql = "DELETE FROM Funcionario WHERE id_Funcionario=" + txtID.Text;
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.CommandType = CommandType.Text; con.Open();
            try
            {
                int i = cmd.ExecuteNonQuery();
                if (i > 0) MessageBox.Show("Registro excluído com sucesso!");
            }
            catch (Exception ex)
            { MessageBox.Show("Erro: " + ex.ToString()); }
            finally { con.Close(); }
            LimpaCampos();
            DesativaCampos();
            btnExcluir.Enabled = false;
            btnPesquisa.Enabled = true;
            txtPesquisa.Enabled = true;
            btnSalvar.Enabled = false;
            btnCancelar.Enabled = false;
            btnNovo.Enabled = true;
        }

        private void btnCancelar_Click_1(object sender, EventArgs e)
        {
            DesativaBotoes();
            btnCancelar.Enabled = false;
            DesativaCampos();
            LimpaCampos();
            txtPesquisa.Enabled = true;
            btnPesquisa.Enabled = true;
            btnNovo.Enabled = true;
            txtPesquisa.Focus();
        }

        private void btnPesquisa_Click_1(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM Funcionario WHERE id_Funcionario='" + txtPesquisa.Text + "'";
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.CommandType = CommandType.Text;
            SqlDataReader reader; con.Open();
            try
            {
                reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    DesativaBotoes();
                    AtivaCampos();
                    txtPesquisa.Enabled = false;
                    txtID.Enabled = false;
                    btnCancelar.Enabled = true;
                    btnSalvar.Enabled = true;
                    btnExcluir.Enabled = true;
                    txtNome.Focus();
                    txtID.Text = reader[0].ToString();
                    txtNome.Text = reader[1].ToString();
                    mskCPF.Text = reader[2].ToString();
                    mskTelefone.Text = reader[3].ToString();
                    txtCargo.Text = reader[4].ToString();
                    txtSalario.Text = reader[5].ToString();
                    novo = false;
                }
                else
                    MessageBox.Show("Nenhum registro encontrado com o Id informado!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro: " + ex.ToString());
            }
            finally { con.Close(); }
        }
    }
}
